import * as rq from 'request-promise';
import env from './../config/env';
import loginUtils from './../core/LoginUtils';


export class QrenService {

    /**
     * 客户确认查询
     * @param res 
     * @param authorization 
     */
    async qren_query(res, authorization) {
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }
        let user_info = loginUtils.decode(authorization);
        let opeUser: string = user_info.username;
        let deliveryId = res.deliveryId;

        let json = await rq.post(`${env.b2bUrl}/portal/control/wsif/getSalesDeliveryInfo`,
            {
                body: {
                    'deliveryId': deliveryId,
                    'opeUser': opeUser,
                    'login.username': env.loginUsername,
                    'login.password': env.loginPwd,
                },
                json: true
            })

        if (json.responseMessage != 'success') {
            return json;
        }
        let baseInfo: any = json.baseInfo;
        let productInfo: any = json.productInfo;
        let responseMessage: string = json.responseMessage;
        let errorMessage: string = json.errorMessage;

        let productList: any[] = [];
        productInfo.map((item, i) => {
            productList[i] = {
                unit: item.unit,
                orderItemSeqId: item.orderItemSeqId,
                productCode: item.productCode,
                deliveryItemSeqId: item.deliveryItemSeqId,
                productName: item.productName,
                relatedItemSeqId: item.relatedItemSeqId,
                originalOrderItemId: item.originalOrderItemId,
                quantity: item.quantity - 0,
                acceptQuantity: item.quantity - 0,
                returnQuantity: 0,
                changeQuantity: 0
            }
        })
        //实收数量 acceptQuantity  退货数量 returnQuantity 换货数量 changeQuantity
        console.log('json__________________', json);
        return { responseMessage: responseMessage, errorMessage: errorMessage, baseInfo: baseInfo, productInfo: productList };
    }

    /**
     * 客户确认收获接口
     * sign_img
     * billcode
     * @param res 
     * @param authorization 
     */
    async qren_sub(res, authorization) {
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }
        let user_info = loginUtils.decode(authorization);
        let opeUser: string = user_info.username;
        let deliveryId = res.deliveryId;
        let sign_img = res.sign_img;
        let productInfo: any = res.productInfo;

        let json = await rq.post(`${env.b2bUrl}/portal/control/wsif/acceptSalesDelivery`,
            {
                body: {
                    'signImg': sign_img,
                    'deliveryId': deliveryId,
                    'opeUser': opeUser,
                    'login.username': env.loginUsername,
                    'login.password': env.loginPwd,
                    'productInfo': productInfo
                },
                json: true
            })

        console.log('json__________________', json);
        return json;
    }


}


let qrenService = new QrenService();
export default qrenService;